<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class payment extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('accounts/payment_model');
		$this->load->model('accounts/accounts_model');	
		$this->load->library('datatables');			
	}
	
	function get(){
		echo $this->payment_model->get();
	}
		
	
	function create_payment()
	{
		$this->data['page_title'] = "Create Payment";
		$meta['page_title'] = "Create Payment";
		$this->data['suppliers']=$this->payment_model->get_suppliers();
		$this->data['banks']=$this->payment_model->get_banks();
		$this->form_validation->set_rules('id_user', 'Supplier', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('date', 'Date', 'required|xss_clean');
		$this->form_validation->set_rules('debit_amount', 'Debit Amount', 'required|xss_clean');
		if ($this->form_validation->run() == true) 
		{
			$id_account = $this->input->post('voucher_no');
			$data = array(
				'id_user' => $this->input->post('id_user'),
				'user_name' => $this->accounts_model->get_partyname($this->input->post('id_user')),
				'voucher_type' => 'payment',
				'voucher_no' => $this->payment_model->get_voucherno($this->input->post('voucher_no')),
				'date' => $this->input->post('date'),
				'payment_type' => $this->input->post('payment_type'),
				'cheque_no' => $this->input->post('cheque_no'),
				'bank_name' => $this->input->post('bank_name'),
				'debit_amount' => $this->input->post('debit_amount'),
				'ip' => $_SERVER['REMOTE_ADDR'],
			);	
			
			if($id_account==''){
				$this->payment_model->insert($data, $id_account);	
			}else{
				$this->payment_model->update_account($data, $id_account);
				
			}
			redirect("accounts/payment/view_payment");
			//check to see if we are creating the user
			//redirect them back to the admin page
			//$this->session->set_flashdata('message', $this->ion_auth->messages());
			//redirect("auth", 'refresh');
		}
		else
		{
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['id_user'] = array(
				'name'  => 'id_user',
				'id'    => 'id_user',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('id_user'),
				
			);

			$this->data['voucher_no'] = array(
				'name'  => 'voucher_no',
				'id'    => 'voucher_no',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('voucher_no'),
			);
	
			$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'class' => 'datepicker',
			'value' => $this->form_validation->set_value('date'),
			);
	
			$this->data['credit_amount'] = array(
			'name'  => 'credit_amount',
			'id'    => 'credit_amount',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('credit_amount'),
			);
		  $this->load->view('common/header', $meta);
			$this->load->view('accounts/payment/create_payment', $this->data);
			$this->load->view('common/footer');
		}
	}
	
	function view_payment(){
		
	$this->data['payments'] = $this->payment_model->get_payments()->result();
	   $meta['page_title'] = "Payment Lists";
	   $this->load->view('common/header', $meta);
	   $this->load->view('accounts/payment/view_payment1');
       $this->load->view('common/footer');
	}
	

	function edit_payment($id)
	{
		
		$this->data['page_title'] = "Edit Payment";
		$meta['page_title'] = "Edit Payment";
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth', 'refresh');
		}

		$user = $this->payment_model->edit_payment($id);
		$user['id_purchase'] = $this->payment_model->get_id_sale($user['voucher_no']);
		$this->data['suppliers']=$this->payment_model->get_suppliers();
		$this->data['banks']=$this->payment_model->get_banks();
		//validate form input
		$this->form_validation->set_rules('id_user', 'Supplier', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('date', 'Date', 'required|xss_clean');
		$this->form_validation->set_rules('debit_amount', 'Debit Amount', 'required|xss_clean');
		if ($this->form_validation->run() == true) 
		{
			
			$data = array(
				'id_user' => $this->input->post('id_user'),
				'user_name' => $this->accounts_model->get_partyname($this->input->post('id_user')),
				'voucher_type' => 'payment',
				'voucher_no' => $this->payment_model->get_voucherno($this->input->post('voucher_no')),
				'payment_type' => $this->input->post('payment_type'),
				'cheque_no' => $this->input->post('cheque_no'),
				'bank_name' => $this->input->post('bank_name'),
				'date' => $this->input->post('date'),
				'debit_amount' => $this->input->post('debit_amount'),
				'ip' => $_SERVER['REMOTE_ADDR'],
			);	
			if($data['payment_type']==0){
				
				$data['cheque_no']='';
				$data['bank_name'] = '';
				
			}
			
			$this->payment_model->update($data, $id);
			redirect("accounts/payment/view_payment");
			//check to see if we are creating the user
			//redirect them back to the admin page
			//$this->session->set_flashdata('message', $this->ion_auth->messages());
			//redirect("auth", 'refresh');
		}
		else
		{
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['id_user'] = array(
				'name'  => 'id_user',
				'id'    => 'id_user',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('id_user'),
				
			);

			$this->data['voucher_no'] = array(
				'name'  => 'voucher_no',
				'id'    => 'voucher_no',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('voucher_no'),
			);
	
			$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'class' => 'datepicker',
			'value' => $this->form_validation->set_value('date'),
			);
	
			$this->data['debit_amount'] = array(
			'name'  => 'debit_amount',
			'id'    => 'debit_amount',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('debit_amount'),
			);
		
			$this->data['user']=$user;
			$this->load->view('common/header', $meta);
			$this->load->view('accounts/payment/edit_payment', $this->data);
			$this->load->view('common/footer');
		}
	}

	
	function get_invoiceno(){
	$invoices=$this->payment_model->get_invoiceno($_REQUEST['id_user']);
	echo  $invoices;exit;	
	
	}



	function delete_payment($id){
		$this->payment_model->delete_payment($id);
		redirect("accounts/payment/view_payment");		
	}

function get_amount(){
	$debit_amount = $this->payment_model->get_debit_amount($_REQUEST['id_purchase']);
	echo $debit_amount;exit; 
}



}
?>